package controllers.participant;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.EventService;
import domain.Event;
import domain.Participant;
import domain.Registration;

@Controller
@RequestMapping("/participant")
public class ParticipantController {
	@Autowired
	private EventService eventService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView save(@RequestParam int eventId) {
		ModelAndView result;
		Event event;
		Collection<Registration> registrations;
		List<Participant> participants = new LinkedList<Participant>();
		event = eventService.findOne(eventId);
		registrations = event.getRegistrations();
		for (Registration r : registrations) {
			participants.add(r.getParticipant());
		}
		result = new ModelAndView("participant/list");
		result.addObject("participants", participants);
		result.addObject("requestURI", "participant/list.do");
		result.addObject("backURI", "/event/administrator/edit.do?eventId="+eventId);

		return result;
	}

}